//coding=utf8

// -------------------------------
// Initialize Data Tables
// -------------------------------

$(document).ready(function() {

    $.fn.configTables = function(callback) {

        var url = $(this).attr('source');
        var ths = $(this).find("th");

        var columns = new Array();

        for (i = 0; i < ths.length; i++) {
            columns.push({ 'mDataProp': $(ths[i]).attr('column')});
        }

        var table = $(this).dataTable({
            "sDom": "<'row'<'col-xs-6'l><'col-xs-6'f>r>t<'row'<'col-xs-6'i><'col-xs-6'p>>",
            "sPaginationType": "bootstrap",
            "oLanguage": {
                "sLengthMenu": "_MENU_ Resultados por página",
                "sSearch": "",
                "sProcessing" : "Processando...",
                "oPaginate": {
                    "sPrevious": "Anterior",
                    "sNext": "Próxima",
                },
                "sInfo": "Exibindo de _START_ a _END_ (Registros: _TOTAL_)"
            },
            "sAjaxSource": url,
            "sServerMethod": "POST",
            "bProcessing": true,
            "bServerSide": true,
            "aoColumns": columns,
            "fnDrawCallback": function (oSettings, json) {
                table.$("tr").click(function (e) {
                    if ($(this).hasClass('row_selected')) {
                        $(this).removeClass('row_selected');
                    }
                    else {
                        table.$('tr.row_selected').removeClass('row_selected');
                        $(this).addClass('row_selected');
                    }

                    if (callback != null)
                        callback();

                });
            },
            
            
        });

        $('.dataTables_filter input').each(function() {
            if ($(this).hasClass('form-control') == false)
                $(this).addClass('form-control').attr('placeholder', 'Pesquisar...');
        });
        $('.dataTables_length select').each(function() {
            if ($(this).hasClass('form-control') == false)
                $(this).addClass('form-control');
        });

        return table;
    };

});

